/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.modules.enterprise.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.commons.lang.StringUtils;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.enterprise.entity.Enterprise;
import org.springblade.modules.enterprise.mapper.EnterpriseMapper;
import org.springblade.modules.enterprise.service.IEnterpriseService;
import org.springblade.modules.enterprise.vo.EnterpriseLableVO;
import org.springblade.modules.enterprise.vo.EnterpriseLegalInfoVO;
import org.springblade.modules.enterprise.vo.EnterpriseVO;
import org.springblade.modules.enterprise.wrapper.EnterpriseWrapper;
import org.springblade.modules.sga.vo.SgaJITLoginVO;
import org.springblade.modules.system.entity.DictItem;
import org.springblade.modules.system.service.IDictItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 服务实现类
 *
 * @author Blade
 * @since 2022-07-19
 */
@Service
public class EnterpriseServiceImpl extends BaseServiceImpl<EnterpriseMapper, Enterprise> implements IEnterpriseService {
	@Autowired
	private IDictItemService dictItemService;

	@Override
	public IPage<EnterpriseVO> selectEnterprisePage(IPage<EnterpriseVO> page, EnterpriseVO bladeEnterprise) {
		return page.setRecords(baseMapper.selectBladeEnterprisePage(page, bladeEnterprise));
	}

	@Override
	public EnterpriseVO getEnterpriseByCode(String enterpriseCode) {
		Enterprise enterprise = baseMapper.getEnterpriseByCode(enterpriseCode);
		if (enterprise != null) {
			return EnterpriseWrapper.build().entityVO(enterprise);
		}
		return null;
	}

	@Override
	public Map<String, Object> findMyLabels(String enterpriseCode) {
		Map<String, Object> map = new HashMap<>();
		Enterprise enterprise = baseMapper.getEnterpriseByCode(enterpriseCode);
		if(enterprise == null) {
			enterprise = new Enterprise();
			enterprise.setEnterpriseCode(enterpriseCode);
			enterprise.setEnterpriseName("非本系统企业");
		}

		map.put("industryType", enterprise.getIndustryType());
		map.put("enterpriseNature", enterprise.getEnterpriseNature());
		map.put("enterpriseSpecial", enterprise.getEnterpriseSpecial());
		map.put("enterpriseScale", enterprise.getEnterpriseScale());

		List<DictItem> industryTypeList = dictItemService.getList("Industry_classification_new");
		List<EnterpriseLableVO> industryTypeVOList = new ArrayList<>();
		Enterprise finalEnterprise = enterprise;
		industryTypeList.forEach(item -> {
			List<String> values = new ArrayList<>();
			if (StringUtils.isNotBlank(finalEnterprise.getIndustryType())) {
				values = Arrays.asList(finalEnterprise.getIndustryType().split(","));
			}
			EnterpriseLableVO vo = new EnterpriseLableVO();
			vo.setLabelName(item.getLabel());
			vo.setLabelValue(item.getValue());
			vo.setStatus("normal");
			if (values.contains(item.getValue())) {
				vo.setSelected(true);
			} else {
				vo.setSelected(false);
			}
			industryTypeVOList.add(vo);
		});

		map.put("PROFESSION_TYPE", industryTypeVOList);

		List<DictItem> enterpriseNatureList = dictItemService.getList("OBJECT_QUALITY");
		List<EnterpriseLableVO> enterpriseNatureVOList = new ArrayList<>();
		Enterprise finalEnterprise1 = enterprise;
		enterpriseNatureList.forEach(item -> {
			List<String> values = new ArrayList<>();
			if (StringUtils.isNotBlank(finalEnterprise1.getEnterpriseNature())) {
				values = Arrays.asList(finalEnterprise1.getEnterpriseNature().split(","));
			}
			EnterpriseLableVO vo = new EnterpriseLableVO();
			vo.setLabelName(item.getLabel());
			vo.setLabelValue(item.getValue());
			vo.setStatus("normal");
			if (values.contains(item.getValue())) {
				vo.setSelected(true);
			} else {
				vo.setSelected(false);
			}
			enterpriseNatureVOList.add(vo);
		});

		map.put("OBJECT_QUALITY", enterpriseNatureVOList);

		List<DictItem> enterpriseScaleList = dictItemService.getList("OBJECT_SCALE");
		List<EnterpriseLableVO> enterpriseScaleVOList = new ArrayList<>();
		Enterprise finalEnterprise2 = enterprise;
		enterpriseScaleList.forEach(item -> {
			List<String> values = new ArrayList<>();
			if (StringUtils.isNotBlank(finalEnterprise2.getEnterpriseScale())) {
				values = Arrays.asList(finalEnterprise2.getEnterpriseScale().split(","));
			}
			EnterpriseLableVO vo = new EnterpriseLableVO();
			vo.setLabelName(item.getLabel());
			vo.setLabelValue(item.getValue());
			vo.setStatus("normal");
			if (values.contains(item.getValue())) {
				vo.setSelected(true);
			} else {
				vo.setSelected(false);
			}
			enterpriseScaleVOList.add(vo);
		});

		map.put("object_scale", enterpriseScaleVOList);

		List<DictItem> enterpriseSpecialList = dictItemService.getList("enterprise_characteristics");
		List<EnterpriseLableVO> enterpriseSpecialVOList = new ArrayList<>();
		Enterprise finalEnterprise3 = enterprise;
		enterpriseSpecialList.forEach(item -> {
			List<String> values = new ArrayList<>();
			if (StringUtils.isNotBlank(finalEnterprise3.getEnterpriseSpecial())) {
				values = Arrays.asList(finalEnterprise3.getEnterpriseSpecial().split(","));
			}
			EnterpriseLableVO vo = new EnterpriseLableVO();
			vo.setLabelName(item.getLabel());
			vo.setLabelValue(item.getValue());
			vo.setStatus("normal");
			if (values.contains(item.getValue())) {
				vo.setSelected(true);
			} else {
				vo.setSelected(false);
			}
			enterpriseSpecialVOList.add(vo);
		});

		map.put("enterprise_characteristics", enterpriseSpecialVOList);

		return map;
	}

	@Override
	public Boolean updateMyLabels(String creditCode, Map<String, List<String>> enterpriseLabel) {
		Enterprise enterprise = baseMapper.getEnterpriseByCode(creditCode);
		if(enterprise == null) {
			return false;
		}

		List<String> industryTypeStringList = enterpriseLabel.get("PROFESSION_TYPE");
		if (industryTypeStringList == null || industryTypeStringList.isEmpty()) {
			enterprise.setIndustryType("");
		} else {
			QueryWrapper<DictItem> industryTypeQueryWrapper = new QueryWrapper<>();
			industryTypeQueryWrapper.in("label", industryTypeStringList);
			industryTypeQueryWrapper.eq("type", "Industry_classification_new");
			List<DictItem> industryTypeList = dictItemService.list(industryTypeQueryWrapper);
			StringBuilder industryTypeString = new StringBuilder();
			industryTypeList.forEach(item -> {
				industryTypeString.append(item.getValue());
				industryTypeString.append(",");
			});
			if (industryTypeString.length() > 0) {
				industryTypeString.delete(industryTypeString.length() - 1, industryTypeString.length());
			}
			enterprise.setIndustryType(industryTypeString.toString());
		}

		List<String> enterpriseNatureStringList = enterpriseLabel.get("OBJECT_QUALITY");
		if (enterpriseNatureStringList == null || enterpriseNatureStringList.isEmpty()) {
			enterprise.setEnterpriseNature("");
		} else {
			QueryWrapper<DictItem> enterpriseNatureQueryWrapper = new QueryWrapper<>();
			enterpriseNatureQueryWrapper.in("label", enterpriseNatureStringList);
			enterpriseNatureQueryWrapper.eq("type", "OBJECT_QUALITY");
			List<DictItem> enterpriseNatureList = dictItemService.list(enterpriseNatureQueryWrapper);
			StringBuilder enterpriseNatureString = new StringBuilder();
			enterpriseNatureList.forEach(item -> {
				enterpriseNatureString.append(item.getValue());
				enterpriseNatureString.append(",");
			});
			if (enterpriseNatureString.length() > 0) {
				enterpriseNatureString.delete(enterpriseNatureString.length() - 1, enterpriseNatureString.length());
			}
			enterprise.setEnterpriseNature(enterpriseNatureString.toString());
		}

		List<String> enterpriseScaleStringList = enterpriseLabel.get("object_scale");
		if (enterpriseScaleStringList == null || enterpriseScaleStringList.isEmpty()) {
			enterprise.setEnterpriseScale("");
		} else {
			QueryWrapper<DictItem> enterpriseScaleQueryWrapper = new QueryWrapper<>();
			enterpriseScaleQueryWrapper.in("label", enterpriseScaleStringList);
			enterpriseScaleQueryWrapper.eq("type", "OBJECT_SCALE");
			List<DictItem> enterpriseScaleList = dictItemService.list(enterpriseScaleQueryWrapper);
			StringBuilder enterpriseScaleString = new StringBuilder();
			enterpriseScaleList.forEach(item -> {
				enterpriseScaleString.append(item.getValue());
				enterpriseScaleString.append(",");
			});
			if (enterpriseScaleString.length() > 0) {
				enterpriseScaleString.delete(enterpriseScaleString.length() - 1, enterpriseScaleString.length());
			}
			enterprise.setEnterpriseScale(enterpriseScaleString.toString());
		}

		List<String> enterpriseSpecialStringList = enterpriseLabel.get("enterprise_characteristics");
		if (enterpriseSpecialStringList == null || enterpriseSpecialStringList.isEmpty()) {
			enterprise.setEnterpriseSpecial("");
		} else {
			QueryWrapper<DictItem> enterpriseSpecialQueryWrapper = new QueryWrapper<>();
			enterpriseSpecialQueryWrapper.in("label", enterpriseSpecialStringList);
			enterpriseSpecialQueryWrapper.eq("type", "enterprise_characteristics");
			List<DictItem> enterpriseSpecialList = dictItemService.list(enterpriseSpecialQueryWrapper);
			StringBuilder enterpriseSpecialString = new StringBuilder();
			enterpriseSpecialList.forEach(item -> {
				enterpriseSpecialString.append(item.getValue());
				enterpriseSpecialString.append(",");
			});
			if (enterpriseSpecialString.length() > 0) {
				enterpriseSpecialString.delete(enterpriseSpecialString.length() - 1, enterpriseSpecialString.length());
			}
			enterprise.setEnterpriseSpecial(enterpriseSpecialString.toString());
		}

		return updateById(enterprise);
	}

	@Override
	public Boolean updateFromJITObject(SgaJITLoginVO jitObject, Long id) {
		return baseMapper.updateFromJITObject(jitObject, id);
	}

	@Override
	public EnterpriseLegalInfoVO getEnterpriseLegalInfoByCode(String enterpriseCode) {
		return baseMapper.getEnterpriseLegalInfoByCode(enterpriseCode);
	}
}
